package com.photoeditor.demo.ad;

import android.content.Intent;
import android.support.annotation.DrawableRes;
import android.support.annotation.LayoutRes;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.TextView;

import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.common.commercialize.ad.AdDisplay;
import com.common.commercialize.ad.AdManager;
import com.common.commercialize.ad.third.IAd;
import com.photoeditor.R;

import com.photoeditor.demo.model.bean.IItemBean;
import com.photoeditor.demo.util.FontUtil;
import com.wv.nw.ui.view.AllinoneAdView;
import com.wv.nw.ui.view.BaseNativeAdView;

import java.io.Serializable;

/**
 * Created by mutour on 2017/5/11.
 */

public class AdItemBean implements IItemBean<IAd>, MultiItemEntity, Serializable {

    private IAd mNativeAdBean;
    private int mLineNum = 0;
    private int mColumnNum = 0;
    private int mRealLineNum = 0;
    private int mAdIndex = 0;
    private int mAdTouchType = 0;
    private boolean mNeedPaddingAdMask = true;
    private boolean mIsNeedLimitAdHeight = false;
    private int mMaxCoverHeight = -1;
    private String mAdCacheKey = null;
    private boolean mIsShowed = false;
    private boolean mNeedRandomActionBackground = false;
    @DrawableRes
    private int mRandomBackgroundId;

    private View mAdView;
    private BaseNativeAdView mBaseNativeView;

    public AdItemBean() {

    }

    public AdItemBean(String adCacheKey) {
        mAdCacheKey = adCacheKey;
        mNativeAdBean = (IAd) AdManager.getInstance().getAd(adCacheKey);
    }

    private OnAdRefreshListener onAdRefreshListener;

    public void setAdItem(AdItemBean itemBean) {
        this.mNativeAdBean = itemBean.get();
        this.mLineNum = itemBean.getLineNum();
        this.mAdIndex = itemBean.getAdIndex();
        this.mAdTouchType = itemBean.getAdTouchType();
        this.mAdCacheKey = itemBean.getAdCacheKey();
    }

    @Override
    public IAd get() {
        return mNativeAdBean;
    }

    @Override
    public void set(IAd data) {
        mNativeAdBean = data;
    }

    @Override
    public int getType() {
        return TYPE_AD;
    }

    @Override
    public void setType(int type) {

    }

    @Override
    public int getDataCount() {
        return 0;
    }

    public int getLineNum() {
        return mLineNum;
    }

    public void setLineNum(int lineNum) {
        mLineNum = lineNum;
    }

    public int getRealLineNum() {
        return mRealLineNum;
    }

    public void setRealLineNum(int lineNum) {
        mRealLineNum = lineNum;
    }

    public int getAdIndex() {
        return mAdIndex;
    }

    public void setAdIndex(int mAdIndex) {
        this.mAdIndex = mAdIndex;
    }

    public int getAdTouchType() {
        return mAdTouchType;
    }

    public void setAdTouchType(int mAdTouchType) {
        this.mAdTouchType = mAdTouchType;
    }

    public String getAdCacheKey() {
        return mAdCacheKey;
    }

    public void setAdCacheKey(String mAdCacheKey) {
        this.mAdCacheKey = mAdCacheKey;
    }

    public void destory() {
        AdManager.getInstance().destoryAd(mAdCacheKey);
    }

    public boolean hasAd() {
        return AdManager.getInstance().hasAd(mAdCacheKey);
    }

    public boolean hasAdAndInvalid() {
        return AdManager.getInstance().hasAdAndInvalid(mAdCacheKey);
    }

    public View createAdView(@LayoutRes int layoutId) {
        AllinoneAdView allinoneAdView = AllinoneAdView.newAllinoneAdView(layoutId, null);
        Intent intent = AdDisplay.makeIntent(mAdCacheKey, mNativeAdBean);
        if (intent != null) {
            intent.putExtra(AdDisplay.KEY_DEFAULT_RES_ID, R.drawable.ic_default_image);
            intent.putExtra(AdDisplay.KEY_LIMIT_AD_HEIGHT, mIsNeedLimitAdHeight);
            if (mMaxCoverHeight != -1) {
                intent.putExtra(AdDisplay.KEY_AD_COVER_MAX_HEIHGT, mMaxCoverHeight);
            }
            intent.putExtra(AdDisplay.KEY_TOUCH_TYPE, mAdTouchType);
        }
        allinoneAdView.setData(intent);
        mAdView = allinoneAdView.getViewAndRemoveParent(true, mNeedPaddingAdMask);
        mBaseNativeView = allinoneAdView.getBaseNativeAdView();
        if (mBaseNativeView != null) {
            TextView adTitleView = mBaseNativeView.getAdTitleView();
            if (adTitleView != null) {
                FontUtil.setCustomFontBold(adTitleView);
            }
            View adActionView = mBaseNativeView.getAdActionView();
            if (adActionView instanceof TextView) {
                FontUtil.setCustomFontBold((TextView) adActionView);
            }
            if (mNeedRandomActionBackground) {
                adActionView.setBackgroundResource(mRandomBackgroundId);
            }
        }
        return mAdView;
    }

    public View getAdView() {
        if (mAdView == null) {
            return null;
        }
        ViewParent parent = mAdView.getParent();
        if (parent != null && parent instanceof ViewGroup) {
            ((ViewGroup) parent).removeView(mAdView);
        }
        return mAdView;
    }

    public BaseNativeAdView getBaseNativeView() {
        return mBaseNativeView;
    }

    public boolean equals(AdItemBean adItemBean) {
        IAd ad = null;
        if(adItemBean != null) {
            ad = adItemBean.get();
        }
        return AdManager.equals(ad, mNativeAdBean);
    }

    public int getColumnNum() {
        return mColumnNum;
    }

    public void setColumnNum(int mColumnNum) {
        this.mColumnNum = mColumnNum;
    }

    public boolean isIsShowed() {
        return mIsShowed;
    }

    public void setIsShowed(boolean mIsShowed) {
        this.mIsShowed = mIsShowed;
    }

    public void setOnAdRefreshListener(OnAdRefreshListener onAdRefreshListener) {
        this.onAdRefreshListener = onAdRefreshListener;
    }

    public void refreshAd() {
        if (onAdRefreshListener != null) {
            onAdRefreshListener.onAdRefresh();
        }
    }

    public void setNeedLimitAdHeight(boolean needLimitAdHeight) {
        mIsNeedLimitAdHeight = needLimitAdHeight;
    }

    public void setNeedPaddingAdMask(boolean needPaddingAdMask) {
        this.mNeedPaddingAdMask = needPaddingAdMask;
    }

    public void setMaxCoverHeight(int maxCoverHeight) {
        this.mMaxCoverHeight = maxCoverHeight;
    }

    public void setNeedRandomButtonBackground(boolean needRandomButtonBackground) {
        this.mNeedRandomActionBackground = needRandomButtonBackground;
    }

    public void setRandomBackgroundId(int randomBackgroundId) {
        this.mRandomBackgroundId = randomBackgroundId;
    }

    @Override
    public int getItemType() {
        return TYPE_AD;
    }

    public interface OnAdRefreshListener {
        void onAdRefresh();
    }
}
